package 离婚登记数和粗离婚率之比;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import 离婚登记数和粗离婚率之比.Bean.DivorceAnalysisBean;
import 离婚登记数和粗离婚率之比.Bean.DivorceAnalysisBean1;

import java.io.IOException;

public class DivorceAnalysisMapper extends Mapper<LongWritable, Text, DivorceAnalysisBean, DivorceAnalysisBean1> {
    private DivorceAnalysisBean outKey = new DivorceAnalysisBean();
    private DivorceAnalysisBean1 outValue = new DivorceAnalysisBean1();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 按照逗号分割字符串，获取各个字段
        //省级,市级,年份,结婚登记(万对),内地居民登记结婚(万对),内地居民初婚登记(万对),内地居民再婚登记(万对),涉外及港澳台居民登记结婚(万对),离婚登记(万对),人口总数（万人）,粗离婚率(‰)
        String[] fields = value.toString().split(",");
        if (fields.length == 10) {
            String year = fields[2];
            String province = fields[0];
            // 只处理2020年和2021年的数据
            if ("2020年".equals(year) || "2021年".equals(year)) {

                float divorceCount = Float.parseFloat(fields[8]);
                long populationCount = Long.parseLong(fields[9]);

                outKey.set(year,province);
                outValue.set(divorceCount,populationCount);

                // 将键值对写入上下文，传递给Reducer
                context.write(outKey, outValue);
            }
        }
    }
}
